package com.turing.data.scripts.contract.filter

import com.turing.data.engine.CombineLongText
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils

/*
调节方式
* */

class ControlMode_30205_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_NEG = /.*协商、调解.*/
        def REGEX_ARB = /.*进行仲裁.*/
        def REGEX_LIT = /.*法院.*/

        def JSONTextMap = dependParams.JSONTextMap
        def textPathMap = dependParams.textPathMap

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        try {
            if (resultVOs!=null && resultVOs.size()>0) {
                for (int i = 0; i < resultVOs.size(); i++) {
                    def resultVO = resultVOs[i]
                    def text = resultVO.text
                    def childTaskSn = resultVO.childTaskSn
                    if(null == childTaskSn || childTaskSn == "" || StringUtils.isEmpty(text)){
                        trueValueSet.add(resultVOs.get(0))
                        return trueValueSet
                    }
                    def start = resultVO.start
                    def end = resultVO.end

                    def rawPath = textPathMap.get(resultVO.childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)
                    String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,20)
                    if ( trueValue =~ REGEX_NEG || trueValue =~ REGEX_ARB || trueValue =~ REGEX_LIT ){
                        if( trueValue =~ REGEX_NEG ){
                            def expandOffset = 0
                            while( expandOffset < 150 && rawText.length() > expandOffset + end ){
                                def expandStr = rawText.substring(start,end+expandOffset)
                                if( expandStr =~ REGEX_ARB ){
                                    def startOffset = expandStr.length() - 4
                                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, startOffset, expandOffset, fileTextStr, rawText)
                                    ObjectNew.text = rawText.substring(start + startOffset, end + expandOffset )
                                    ObjectNew.businessValue = rawText.substring(start + startOffset, end + expandOffset)
                                    trueValueSet.add(resultVO)
                                    trueValueSet.add(ObjectNew)
                                    printf trueValueSet.toString()
                                    return trueValueSet
                                }
                                expandOffset ++
                            }

                            def expand = 0
                            while( expand < 150 && rawText.length() > expand + end ){
                                def expandString = rawText.substring(start,end+expand)
                                if(  expandString =~ REGEX_LIT ){

                                    def startOffset = expandString.length() - 2
                                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, startOffset, expand, fileTextStr, rawText)
                                    ObjectNew.text = rawText.substring(start + startOffset, end + expand )
                                    ObjectNew.businessValue = rawText.substring(start + startOffset, end + expand)
                                    println ObjectNew.text = rawText.substring(start + startOffset, end + expand )
                                    println startOffset
                                    println expandString.length()
                                    println expandString
                                    trueValueSet.add(resultVO)
                                    trueValueSet.add(ObjectNew)
                                    return trueValueSet
                                }
                                expand ++
                            }

                        }
                        if( trueValue =~ REGEX_LIT ){
                            def expand = 0
                            while( expand < 150 && start > expand  ){
                                def expandString = rawText.substring(start-expand,end)
                                if( expandString =~ REGEX_NEG ){
                                    def endOffset = expandString.length() - 5
                                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, -expand, -endOffset, fileTextStr, rawText)
                                    ObjectNew.text = rawText.substring(start-expand, end - endOffset )
                                    ObjectNew.businessValue = rawText.substring(start-expand, end - endOffset)
                                    trueValueSet.add(ObjectNew)
                                    trueValueSet.add(resultVO)
                                    return trueValueSet
                                }
                                expand ++
                            }
                        }
                        if( trueValue =~ REGEX_ARB ){
                            def expand = 0
                            while( expand < 150 && start > expand  ){
                                def expandString = rawText.substring(start-expand,end)
                                if( expandString =~ REGEX_NEG ){
                                    def endOffset = expandString.length() - 5
                                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, -expand, -endOffset, fileTextStr, rawText)
                                    ObjectNew.text = rawText.substring(start-expand, end - endOffset )
                                    ObjectNew.businessValue = rawText.substring(start-expand, end - endOffset)
                                    trueValueSet.add(ObjectNew)
                                    trueValueSet.add(resultVO)
                                    return trueValueSet
                                }
                                expand ++
                            }
                        }
                        trueValueSet.add(resultVOs.get(i))
                        return trueValueSet
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace()
        }



        return  resultVOs
    }

}
